/* ==================================================================== *
 *
 * This file is part of Mapics Framework.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author Andrey Cherkashin <action.open@gmail.com>
 *
 * ==================================================================== */

/**
 * Constructs overlay for google map layer
 *
 * Here we make inheritance in function (not prototype = ...) because of
 * we have no google.maps.OverlayView class definition before requesting for
 * google maps api javascript. So, if we will load this file and trying to
 * inheritance from this class, we will get error.
 *
 * @param level (int) Level of this overlay
 * @param pMap  (google.maps.Map) Google Map pointer
 */
mapics.MapLayerGoogleOverlay = function(level, pMap)
{
    var self = this;

    /**
     * Adding overlay div in map container
     */
    this.m_Div = this.createDiv(level);

    /**
     * List of map objects
     */
    this.m_MapObjects = [];

    /**
     * Google maps aggregated overlay
     *
     * We do it for make inheritance from mapics.MapLayerOverlay, because
     * of real overlay need to inherit google.map.OverlayView class
     */
    this.googleOverlayPrototype = function()
    {
        var selfOverlay = this;
        
        /** ------------------------------------------------------------------------- **
         *  EVENTS
         ** ------------------------------------------------------------------------- **/

        /**
         * Initializes when overlay is added on map
         */
        this.onAdd = function()
        {
            self.m_Div.style.display = "block";
            selfOverlay.getPanes().overlayLayer.appendChild(self.m_Div);
        };

        /**
         * Initializes when overlay is removed from map
         */
        this.onRemove = function()
        {
            self.m_Div.style.display = "none";
            selfOverlay.getPanes().overlayLayer.removeChild(self.m_Div);
        };

        /**
         * Initializes when overlay is drawing
         */
        this.draw = function()
        {
            for(var i = 0, max = self.m_MapObjects.length; i < max; i++)
            {
                var pos = selfOverlay.getProjection().fromLatLngToDivPixel(
                    self.m_MapObjects[ i ].getPosition("google")
                );

                if( (offset = self.m_MapObjects[ i ].getOffset()) ) {
                    pos.x += offset.x;
                    pos.y += offset.y;
                }

                self.m_MapObjects[ i ].getDiv().style.left = pos.x + "px";
                self.m_MapObjects[ i ].getDiv().style.top  = pos.y + "px";
            }
        };
    };
    
    this.googleOverlayPrototype.prototype = new google.maps.OverlayView();

    this.m_OverlayObject = new this.googleOverlayPrototype();
    this.m_OverlayObject.setMap( pMap );
};

/**
 * Inheritance from mapics.MapLayerOverlay
 */
mapics.MapLayerGoogleOverlay.prototype = new mapics.MapLayerOverlay();

/** ------------------------------------------------------------------------- **
 *  ACTIONS
 ** ------------------------------------------------------------------------- **/

/**
 * Updates this map overlay objects
 *
 * @return mapics.MapLayerGoogleOverlay
 */
mapics.MapLayerGoogleOverlay.prototype.update = function()
{
    // Update overlay if we already loaded the overlay
    if( this.m_OverlayObject.getProjection() ) {
        this.m_OverlayObject.draw();
    }
};

